<template>
  <el-dialog
    :destroy-on-close="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="100px" :model="form">
      <el-form-item label="模板组名称" prop="group_name">
        <el-input v-model="form.group_name" />
      </el-form-item>
      <el-form-item label="模板组标识" prop="group_mark">
        <el-input v-model="form.group_mark" />
      </el-form-item>
      <!-- <el-form-item label="通知人群类型" prop="notify_type">
        <el-select v-model="form.notify_type" placeholder="请选择通知人群类型">
          <el-option
            v-for="item in dictList"
            :key="item.item_code"
            :label="item.item_name"
            :value="item.item_code"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="触发方法名" prop="func_name">
        <el-input v-model="form.func_name" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          v-model="form.sort"
          :step="1"
          step-strictly
          @change="numberChange"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { editMessageTpl, addMessageTpl } from '@/api/message'
  // import { getDictByCode } from '@/utils/dict'

  export default {
    name: 'MsgEdit',
    data() {
      return {
        form: {
          sort: 0,
          notify_type: 'all',
        },
        title: '',
        dialogFormVisible: false,
        list: [],
        dictList: [],
      }
    },
    methods: {
      numberChange(value) {
        this.form.sort = value
        this.$forceUpdate()
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.group_id = row.id
          this.form.sort = row.group_sort
        }
        this.dialogFormVisible = true
        this.$forceUpdate()
      },
      // async getDict() {
      //   this.dictList = await getDictByCode('message_send_type')
      // },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },

      async save() {
        if (this.title == '添加') {
          const { data } = await addMessageTpl(this.form)
          if (data.is_success) {
            this.$baseMessage('添加成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
          }
        } else if (this.title == '编辑') {
          const { data } = await editMessageTpl(this.form)
          if (data.is_success) {
            this.$baseMessage('修改成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
          }
        }
        this.close()
      },
    },
  }
</script>
